package cn.e2win.biz.comm;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import cn.e2win.biz.sys.Dict;

public class ExcelTpl {
	
	private static void initMap(Object keys){
		String key = (String)keys;
		if(map==null){
			map = new HashMap<String, ExcelTpl>();
		}		
			
		if(!map.containsKey(key)){
			String title;
			List<Cell> cellList = new ArrayList<Cell>();
			//获取XMML解析器
			SAXReader sax = new SAXReader();
			try {
				String path = Dict.getRealPath()+"/WEB-INF/view/" + key + ".xml";
				Document doc = sax.read(new File(path));
				//获得根节点
				Element root = doc.getRootElement();
				//获得文件名称
				title = root.attributeValue("title");
				//获得根节点下所有节点
				Iterator iter = root.elementIterator();
				while(iter.hasNext()){
					Element inkmen = (Element) iter.next();
					//判断如果condictionValue值为NOT_1时则不显示
					//if(!"NOT_1".equals(inkmen.attributeValue("condictionValue"))){
						String header =  inkmen.elementText("header");
						String filed = inkmen.elementText("filed");
						String dataTyp = inkmen.elementText("dataTyp");
						if(dataTyp==null||dataTyp==""){
							cellList.add(new Cell(header,filed ));
						}else{
							cellList.add(new Cell(header,filed, dataTyp));
						}
					//}
					
				}
			}catch(DocumentException  e){
				throw new RuntimeException("解析文件失败",e);
			}
			Map<Integer, Cell> countRow = new HashMap<Integer, Cell>();
			map.put(key, new ExcelTpl(title,cellList,countRow));

		}
				
	}
	
	
	
	
	
	
	
	
	
	
	
	
	

	public static final String DICT = "DICT:";
	public static final String FORMULA = "FORMULA:";
	private static Map<String,ExcelTpl> map = null;
	private String title;
	private List<Cell> cellList;
	private Map<Integer,Cell> countRow;
	
	public ExcelTpl(){
		
	}
	public ExcelTpl(String title, List<Cell>cellList){
		this.title = title;
		this.cellList = cellList;
	}
	
	public ExcelTpl(String title, List<Cell>cellList, Map<Integer, Cell>countRow){
		this.title = title;
		this.cellList = cellList;
		this.countRow = countRow;
	}
	
	public static ExcelTpl getTpl(Object key){
		initMap(key);
		return map.get(key);
	}
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public List<Cell> getCellList() {
		return cellList;
	}
	public void setCellList(List<Cell> cellList) {
		this.cellList = cellList;
	}
	public Map<Integer, Cell> getCountRow() {
		return countRow;
	}
	public void setCountRow(Map<Integer, Cell> countRow) {
		this.countRow = countRow;
	}
	
}
